package com.huiyujiaoyu.powertime.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.AppCompatRatingBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleBean;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.LlkjToolBar;
import com.huiyujiaoyu.widget.SetHeightWebUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.huiyujiaoyu.http.HttpMethods.getMD5Pare;


/**
 * Created by hhwly on 2017-06-29.
 */

public class ArticleDetailsActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.tv_desc_title)
    TextView mTvDescTitle;
    @BindView(R.id.tv_read_count)
    TextView mTvReadCount;
    @BindView(R.id.ll_title)
    LinearLayout mLlTitle;
    @BindView(R.id.writer)
    TextView mWriter;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.ll_info)
    LinearLayout mLlInfo;
    @BindView(R.id.wv_content)
    WebView mWvContent;
    @BindView(R.id.ll_desc_default)
    LinearLayout mLlDescDefault;
    @BindView(R.id.iv_icon)
    ImageView mIvIcon;
    @BindView(R.id.tv_author_name)
    TextView mTvAuthorName;
    @BindView(R.id.tv_dingyueshu)
    TextView mTvDingyueshu;
    @BindView(R.id.ll_column)
    LinearLayout mLlColumn;
    @BindView(R.id.tv_dingyue)
    TextView mTvDingyue;
    @BindView(R.id.tv_yidingyue)
    TextView mTvYidingyue;
    @BindView(R.id.rl_column)
    RelativeLayout mRlColumn;
    @BindView(R.id.rc_view)
    RecyclerView mRcView;
    @BindView(R.id.tv_comment)
    TextView mTvComment;
    @BindView(R.id.ibtn_comment)
    ImageButton mIbtnComment;
    @BindView(R.id.rl_comment)
    RelativeLayout mRlComment;
    @BindView(R.id.ll_default)
    LinearLayout mLlDefault;
    @BindView(R.id.ls_view)
    RecyclerView mLsView;
    @BindView(R.id.btn_more)
    Button mBtnMore;
    private String wzurl;
    private List<IndexItemModel> qtzpdatas,pldatas;
    private BaseQuickAdapter<IndexItemModel,BaseViewHolder> qtzpadapter,pladapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_article_details;
    }
    public static void launch(Context context, String content_id) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.DATA,content_id);
        ActivityUtil.startActivity(context, ArticleDetailsActivity.class,bundle);
    }
    private String content_id;

    public void initView() {
        setToolbar(mToolbar);
        mToolbar.setToolBar("", true, "", R.mipmap.icon_titlebar_back, true, "", R.mipmap.icon_titlebar_share);
        mToolbar.setRTVisible();
    }

    public void initData() {
        if (!getIntent().hasExtra(Constant.DATA)) {
            return;
        }
        content_id = getIntent().getStringExtra(Constant.DATA);
        HttpMethods.getInstance().view(mContext, getComp(), this, content_id, "0", 1);
        qtzpdatas = new ArrayList<>();
        pldatas = new ArrayList<>();
        qtzpadapter =  new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(R.layout.other_recler,qtzpdatas) {
            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel item) {

            }
        };
        pladapter =  new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(R.layout.comment_list,pldatas) {
            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel item) {
                GlideUtil.loadCircleImg((ImageView) helper.getView(R.id.cv_head), StringUtils.getFullUrl(item.photo));
                helper.setText(R.id.tv_name, item.name);
                helper .setText(R.id.tv_time, item.created_at);
                helper .setText(R.id.tv_desc, item.content);
                AppCompatRatingBar rb = helper.getView(R.id.ratbar);
                rb.setRating(item.score);
            }
        };
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRcView.setLayoutManager(linearLayoutManager);
        mRcView.setAdapter(qtzpadapter);
        mLsView.setLayoutManager(new LinearLayoutManager(mContext));
        mLsView.setAdapter(pladapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mToolbar.setRightButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mToolbar.setRightButton2OnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void getUrl() {
        TreeMap<String, Object> map = new TreeMap<>(new Comparator<String>() {
            public int compare(String obj1, String obj2) {
                // 降序排序
                return obj1.compareTo(obj2);
            }
        });
        map.put("content_id", content_id);
        map.put("user_id", UserInfoBean.getInstance().getUid());
        map.put("type", 1);
        String timestamp = System.currentTimeMillis() + "";
        map.put("timestamp", timestamp);
        String code = getMD5Pare(map);
        wzurl = String.format(Constant.WZXQ, content_id, UserInfoBean.getInstance().getUid(), timestamp, code);
    }

    @OnClick({R.id.ibtn_comment, R.id.btn_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_comment:
                break;
            case R.id.btn_more:
                CommentActivity.launch(mContext,content_id,pid,column_id);
                break;
        }
    }
    SimpleBean dataModel;
    boolean isOrder,isPay;
    private SetHeightWebUtil setHeightWebUtil;
    private int column_id;
    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_VIEW:
                dataModel = (SimpleBean) o;
                //收藏
                mToolbar.setRightImg2( "1".equals(dataModel.collected)?R.mipmap.icon_titlebar_collection_s:R.mipmap.icon_titlebar_collection_n);
                //订阅
                isOrder = "1".equals(dataModel.subscribed);
                mTvDingyue.setText(isOrder?"已订阅":"订阅");
                //是否免费
                isPay = 1==dataModel.status;
                //评论
                getUrl();
                setHeightWebUtil = new SetHeightWebUtil(mWvContent);
                setHeightWebUtil.setDoWhatListener(this);
                setHeightWebUtil.loadUrl(wzurl);
                initColumn(dataModel.column);
                initContent(dataModel.content);
                break;
            case HttpApi.HTTP_COMMENT_INDEX:
                pldatas.addAll(((SimpleListBean) o).list);
                pladapter.notifyDataSetChanged();
                break;
        }
    }
    private void initColumn(IndexItemModel column) {

        mTvTime.setText(column.created_at);
        mTvAuthorName.setText(column.title);
        mTvDingyueshu.setText(column.subscribers+"人订阅");
        mWriter.setText(column.name);
    }

    private int pid;

    private void initContent(IndexItemModel contentData) {

        mTvReadCount.setText(contentData.view_count+"人阅读");
        mTvDescTitle.setText(contentData.title);
        GlideUtil.loadImg(mIvIcon,StringUtils.getFullUrl(contentData.headimg));
        column_id = contentData.column_id;
        pid = contentData.pid;
        //评论
        if (contentData.comment_count > 0) {
            HttpMethods.getInstance().cIndex(mContext,getComp(),this,content_id,column_id+"","0",1);
            mBtnMore.setVisibility(View.VISIBLE);
        }else{
            mBtnMore.setVisibility(View.GONE);
        }
    }

}
